'use strict';
const BaseService = require('./base');
const rp = require('request-promise');
const StringUtil = require('../utils/StringUtil');
const Constant = require('../config/Constant');
const OperatorType = require('../config/OperatorType');
const ResponseUtil = require('../utils/ResponseUtil');
const RCEM = require('../utils/ResultCodeEnumMsg');


class BankService extends BaseService {



    async addBank() {
        let loginUserInfo = this.ctx.getLoginInfo();
        let operatorType = loginUserInfo.operatorType;
        let operatorId = loginUserInfo.operatorId;
        let createBy = loginUserInfo.createBy;
        let {username, idcard,bankName,bankCode,cardNumber,phone} = this.ctx.request.body;

        if(!username || !idcard || !bankName || !bankCode || !cardNumber || !phone){
            return ResponseUtil.resError('参数错误');
        }

        let params = {
            id:StringUtil.getUUIDV2(),
            user_id:operatorId,
            username:username,
            idcard:idcard,
            bank_name:bankName,
            bank_code:bankCode,
            card_number:cardNumber,
            phone:phone
        };

        await this.executeInsert('t_user_bank',params);

        let sql = 'update t_user set phone=? where id=?';
        await this.executeSqlSucc(sql,[phone,operatorId]);

        return 1;
    }

    async getBankList(){
        let loginUserInfo = this.ctx.getLoginInfo();
        let operatorType = loginUserInfo.operatorType;
        let operatorId = loginUserInfo.operatorId;
        let createBy = loginUserInfo.createBy;
        let sql = 'select * from t_user_bank where user_id=? and valid=1 order by create_time desc ';
        const list = await this.executeSql(sql,[operatorId]);
        return list;
    }

    async getBankDetail(){
        let loginUserInfo = this.ctx.getLoginInfo();
        let operatorType = loginUserInfo.operatorType;
        let operatorId = loginUserInfo.operatorId;
        let createBy = loginUserInfo.createBy;
        let id = this.ctx.params.id;
        let sql = 'select * from t_user_bank where user_id=? and id=? and valid=1 ';
        return await this.executeSqlFirst(sql,[operatorId,id]);
    }


}

module.exports = BankService;
